package edu.sctu.zhengsenwen.month201506.day20150619;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * Created by user15 on 2015/6/19.
 */
public class PostRequest {

    BufferedReader inputStream;

    public void getPostRequest(String path, String param) throws IOException {

        URL url = new URL(path);

        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

        httpURLConnection.setRequestMethod("POST");
        httpURLConnection.connect();
        httpURLConnection.setDoOutput(true);
        httpURLConnection.setDoInput(true);

        OutputStream outputStream = httpURLConnection.getOutputStream();
        byte[] buffer = new byte[1024];
        outputStream.write(buffer);
        outputStream.close();

        try {
            //读取响应

            if (httpURLConnection.getResponseCode() == httpURLConnection.HTTP_OK) {

                StringBuffer content = new StringBuffer();
                String tempString = "";
                inputStream = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));

                while ((tempString = inputStream.readLine()) != null) {
                    content.append(tempString);
                }
                return;
            } else {
                throw new Exception("请求出现了问题!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
            httpURLConnection.disconnect();
        }
        return;
    }


//public class HttpClient {
//    //发送一个GET请求
//    public static String get(String path) throws Exception{
//        HttpURLConnection httpConn=null;
//        BufferedReader in=null;
//        try {

//    //发送一个GET请求,参数形式key1=value1&key2=value2...
//    public static String post(String path,String params) throws Exception{


    public static void main(String[] args) throws Exception {

        PostRequest postRequest = new PostRequest();
        postRequest.getPostRequest("http://www.baidu.com", "");
    }
}
